var deletedJson = [];
			var changedFlag = false;
			var store;

			Ext.onReady(function(){
				Ext.QuickTips.init();
				
				//var data;

                var sm = new Ext.grid.CheckboxSelectionModel();
				var store = new Ext.data.Store({
					url: 'Tax!queryTax.action',
					reader: new Ext.data.JsonReader({
						root:'result.result',
						id: 'tax_code',
						totalProperty: 'result.totalCount'
					},[
						{name:'tax_code',mapping:'tax_code'},
						{name:'tax_name',mapping:'tax_name'},
						{name:'tax_deduct',mapping:'tax_deduct'},
						{name:'tax_rate',mapping:'tax_rate'}
					])
				  });
				  var cm = new Ext.grid.ColumnModel([
					new Ext.grid.RowNumberer(),
					sm,
					{header: "税率名称", dataIndex: 'tax_name'},
					{header: "起征点", dataIndex: 'tax_deduct'},
					{header: "税率", dataIndex: 'tax_rate'}
				  ]);

                var addWin = getAddWindow(store);

				var addAction = new Ext.Action({
					text:'新增税率',
					//iconCls:'all',
					handler:function() {
						addWin.show();
					}
				});

				var editAction = new Ext.Action({
					text:'修改税率',
					//iconCls:'all',
					handler:function() {
						var length = grid.getSelectionModel().getSelections().length
						if (length < 1){
							Ext.Msg.alert('警告','请选择需要修改的税率');
							return;
						}
						if (length > 1){
							Ext.Msg.alert('警告','只能选择一个税率进行修改');
							return;
						}
						Ext.Ajax.request({
									url:"Tax!queryTaxDetails.action?code=" + grid.getSelectionModel().getSelections()[0].get('tax_code'),
									method:"post",
									success: function(response,options){
											var responseTextVale = Ext.util.JSON.decode(response.responseText);
											addWin.findById('tax_code').setValue(responseTextVale.resultL[0].tax_code);
											addWin.findById('tax_name').setValue(responseTextVale.resultL[0].tax_name);
											addWin.findById('tax_deduct').setValue(responseTextVale.resultL[0].tax_deduct);
											if (responseTextVale.resultL[0].tax_rate == null) {
												addWin.findById('fixedRate').setValue(0);
												addWin.findById('tax_rate').disable();
											} else {
												addWin.findById('fixedRate').setValue(1);
												addWin.findById('tax_rate').enable();
												addWin.findById('tax_rate').setValue(responseTextVale.resultL[0].tax_rate);
											}
											addWin.findById('entryGrid').getStore().loadData(responseTextVale.resultL[0].details,false);
										}	
								});
						addWin.show();
					}
				});

				var delAction = new Ext.Action({
					text:'删除税率',
					//iconCls:'all',
					handler:function() {
						//Ext.Msg.alert('警告','抱歉，目前尚不支持此功能，请期待下一个版本！');
						
						var length = grid.getSelectionModel().getSelections().length;
						if (length > 0){
							Ext.MessageBox.show({
								title:'提示',
								msg:'您选中了' + length + '条记录进行删除,请问您确认吗?',
								buttons:Ext.MessageBox.OKCANCEL,
								fn:function(btn) {
									if (btn == "ok"){
										var id = [];
										var rs = grid.getSelectionModel().getSelections();
										for (var i = 0;i < length ; i++){
											if (rs[i].data.tax_code != "") {
												id.push(rs[i].data.tax_code);
											}
										}
										Ext.Ajax.request({
											url:'Tax!delTax.action',
											method:'post',
											params:{
												tax: Ext.encode(id) 
											},
											success: function(){
												grid.getStore().reload();
											},
											failure: function(){
												Ext.Msg.alert('警告','由于异常原因，提交数据失败！');
											}
										});
									}
									if (btn == "cancel"){
										return;
									}
								}
							});
						} else {
							Ext.Msg.alert('警告','请选择需要删除的记录.');
						}
					}

					
				});

				var queryKeyField = new Ext.form.TextField({ 
					emptyText : '需要查询的税率模版名称',
					fieldLabel: '名称',
					labelWidth: 100,
					name : 'queryKey',
					id : 'queryKey',
					width:200
				});

				var queryAction = new Ext.Action({
					text:'查询',
					//iconCls:'all',
					handler:function() {
						store.on('beforeload' , function(){
      						store.baseParams = {
      							queryKey:queryKeyField.getValue()
							}
						});
						store.reload();
					}
				});


			  var grid = new Ext.grid.GridPanel({
				height : document.body.clientHeight,
				//
				monitorResize:true,
				id:'taxesGrid',
				tbar:[new Ext.form.Label({
						text : '税率模版名称:'
					}),
					' ',
					queryKeyField,
					' ', 
					queryAction,
					'-',
					addAction, 
					' ', 
					editAction, 
					' ', 
					delAction],
				sm:sm,
				cm:cm,
				store:store,
				loadMask: true,
				loadMask:{msg:'正在加载数据...'},
				bbar:new Ext.PagingToolbar({
						pageSize:pageSize,
						store:store,
						displayInfo:true,
						dispalyMsg:'显示第{0}条到{1}条记录，一共{2}条',
						emptyMsg:'没有记录'
					}),
				 autoExpandColumn:2
		      });
			  
			  store.reload();
			  grid.render(Ext.getBody());
			  //new Ext.Toolbar({
                //        renderTo : grid.tbar,   //添加多个工具栏的容器
                  //      items : [{
                    //                text : "查询"
                      //          }]
                    //}) 

			});

			function getAddWindow(taxesstore) {
				var sm = new Ext.grid.CheckboxSelectionModel();
				var store = new Ext.data.Store({
				//url: 'Tax!queryTax.action',
				pruneModifiedRecords:true,
				proxy:new Ext.data.MemoryProxy(Ext.util.JSON.decode("[]")),
				reader: new Ext.data.JsonReader({
					//root:'result',
					//id: 'tax_code',
					totalProperty: 'total'
				},[
					{name:'tax_entry_code',mapping:'tax_entry_code'},
					{name:'tax_code',mapping:'tax_code'},
					{name:'tax_level',mapping:'tax_level'},
					{name:'min_amount',mapping:'min_amount'},
					{name:'max_amount',mapping:'max_amount'},
					{name:'tax_rate',mapping:'tax_rate'},
					{name:'quick_deduct',mapping:'quick_deduct'},
					{name:'tax_releasedate',mapping:'tax_releasedate'},
					{name:'tax_enddate',mapping:'tax_enddate'}
				])
			  });
			  
			  var cm = new Ext.grid.ColumnModel([
			    new Ext.grid.RowNumberer(),
				sm,
				{header: "级别", 
				 width: 50, 
				 dataIndex: 'tax_level',
				 editor:new Ext.grid.GridEditor(
					new Ext.form.NumberField({
						allowBlank:false,
						allowNegative:false,
						minValue:1,
						maxValue:20,
						decimalPrecision:0
						})
					)
				},
				{header: "下限", 
				 width: 100, 
				 dataIndex: 'min_amount',
				 editor:new Ext.grid.GridEditor(
					new Ext.form.NumberField({
						allowNegative:false,
						decimalPrecision:2
						})
					)
				},
				{header: "上限", 
				 width: 100, 
				 dataIndex: 'max_amount',
				 editor:new Ext.grid.GridEditor(
					new Ext.form.NumberField({
						allowNegative:false,
						decimalPrecision:2
						})
					)
				},
				{header: "税率", 
				 width: 100, 
				 dataIndex: 'tax_rate',
				 editor:new Ext.grid.GridEditor(
					new Ext.form.NumberField({
						allowNegative:false,
						decimalPrecision:3,
						minValue:0,
						maxValue:1
						})
					)
				},
				{header: "速算值", 
				 width: 100, 
				 dataIndex: 'quick_deduct',
				 editor:new Ext.grid.GridEditor(
					new Ext.form.NumberField({
						allowNegative:false,
						decimalPrecision:2
						})
					)
				},
				{header: "生效日期", 
				 width: 100, 
				 dataIndex: 'tax_releasedate',
				 type:'date',
				 dataFormat:'Y-m-d',
				 //renderer:Ext.util.Format.dateRenderer('Y-m-d'),
				 renderer:function(value,p,record){
						if (value && typeof(value) == 'string'){
							return  Ext.util.Format.date(getDate(value,'yyyy-MM-ddTHH:mm:ss'),'Y-m-d');
						}
						if (value && typeof(value) == 'object'){
							return Ext.util.Format.date(value,'Y-m-d');
						}
						return value;
					},
				 editor:new Ext.grid.GridEditor(
					new Ext.form.DateField({
						format:'Y-m-d'
						})
					)
				 },
				{header: "终止日期", 
				 width:100, 
				 dataIndex: 'tax_enddate',
				 type:'date',
				 dataFormat:'Y-m-d',
				 renderer:function(value,p,record){
						if (value && typeof(value) == 'string'){
							return  Ext.util.Format.date(getDate(value,'yyyy-MM-ddTHH:mm:ss'),'Y-m-d');
						}
						if (value && typeof(value) == 'object'){
							return Ext.util.Format.date(value,'Y-m-d');
						}
						return value;
					},
				 editor:new Ext.grid.GridEditor(
					new Ext.form.DateField({
						format:'Y-m-d'
						})
					)
				}
			  ]);
              
			  var TaxEntry = Ext.data.Record.create([
				{name:'tax_entry_code',type:'int'},
				{name:'tax_code',type:'int'},
				{name:'tax_level',type:'int'},
				{name:'min_amount',type:'float'},
				{name:'max_amount',type:'float'},
				{name:'tax_rate',type:'float'},
				{name:'quick_deduct',type:'float'},
				{name:'tax_releasedate',type:'date',v:new Date()},
				{name:'tax_enddate',type:'date'}
			  ]);

			  var addAction = new Ext.Action({
				text:'新增',
				//iconCls:'all',
				handler:function() {
					var initValue = {tax_entry_code:'',tax_code:addPanel.findById('tax_code').getValue(),tax_level:'',min_amount:'',max_amount:'',tax_rate:'',quick_deduct:'0',tax_releasedate:new Date(),tax_enddate:''};
					var p = new TaxEntry(initValue);
					//alert(Ext.encode(p));
					entryGrid.stopEditing();
					store.insert(0,p);
					entryGrid.startEditing(0,2);
					entryGrid.view.refresh();
					p.dirty=true;
					p.modified = initValue;
					if (store.modified.indexOf(p) == -1) {
						store.modified.push(p);
					}
				}
			  });

			  var delAction = new Ext.Action({
				text:'删除',
				handler:function(){
					var length = entryGrid.getSelectionModel().getSelections().length;
					if (length > 0){
						Ext.MessageBox.show({
							title:'提示',
							msg:'您选中了' + length + '条记录进行删除,请问您确认吗?',
							buttons:Ext.MessageBox.OKCANCEL,
							fn:function(btn) {
								if (btn == "ok"){
									var rs = entryGrid.getSelectionModel().getSelections();
									for (var i = 0;i < length ; i++){
										if (rs[i].data.tax_entry_code != "") {
											deletedJson.push(rs[i].data);
										}
										store.remove(store.getById(rs[i].id));
									};
								}
								if (btn == "cancel"){
									return;
								}
							}
						});
					} else {
						Ext.Msg.alert('警告','请选择需要删除的记录.');
					}
				}
			  });

			  var entryGrid = new Ext.grid.EditorGridPanel({
			    clicksToEdit:1,
				height:210,
				width:750,
				id:'entryGrid',
				tbar:[addAction,delAction],
				sm:sm,
				cm:cm,
				store:store,
				loadMask: true,
				loadMask:{msg:'正在加载数据...'},
				autoExpandColumn:2
		      });

              store.load();

			  var addPanel = new Ext.form.FormPanel({
					//labelAlign: 'top',
					frame:true,
					title: '模版信息',
					id:'addPanelForm',
					bodyStyle:'padding:5px 5px 0',
					url:'Tax!saveTax.action',
					items: [{
						layout:'column',
						items:[{
						    columnWidth:1,
							layout:'form',
							items:[{
								xtype:'hidden',
								name:'tax_code',
								id:'tax_code'
							}]
						},{
							columnWidth:.5,
							layout: 'form',
							items: [{
								xtype:'textfield',
								fieldLabel: '名称',
								name: 'tax_name',
								id:'tax_name',
								anchor:'95%',
								allowBlank:false,
								blankText:'名称不能为空',
								emptyText:'请输入名称'
							}, {
								xtype:'checkbox',
								fieldLabel: '固定税率',
								name: 'fixedRate',
								id:'fixedRate',
								align:'left',
								anchor:'5%',
								listeners:{
									"check":function(f,n){
										if (n){
											addPanel.findById('tax_rate').enable();
										} else {
											addPanel.findById('tax_rate').disable();
										}
									}
								}
							}]
						},{
							columnWidth:.5,
							layout: 'form',
							items: [{
								xtype:'numberfield',
								fieldLabel: '起征点',
								name: 'tax_deduct',
								id:'tax_deduct',
								anchor:'95%',
								decimalPrecision:2,
								allowNegative:false,
								minValue:0,
								allowBlank:false,
								emptyText:'请输入起征点',
								blankText:'起征点不能为空'
							},{
								xtype:'numberfield',
								fieldLabel: '固定税率',
								name: 'tax_rate',
								id: 'tax_rate',
								anchor:'95%',
								decimalPrecision:3,
								allowNegative:false,
								minValue:0,
								maxValue:1,
								allowBlank:true,
								emptyText:'请输入固定税率',
								disabled:true
							}]
						}]
					}]
				});

				addOnChangedEvent(addPanel);

				var addWin = new Ext.Window({
					buttonalign : 'right',
					closable: true,
					draggable : true,
					height : 450,
					//layout : 'border',
					region : 'center',
					plain : true,
					items : [addPanel,entryGrid],
					//title : '查询批号信息',
					width : 750,
					listeners : {"beforedestroy" : function(obj) {
							//obj.hide();
							return false;
						},"beforeshow":function(obj) {
							deletedJson = [];
							return true;
						},"beforehide":function(obj) {
						    changedFlag = false;
							addPanel.getForm().reset();
							store.loadData(Ext.util.JSON.decode("[]"),false);
							return true;
						}
					},
					buttons : [{
						text:'确认',
						id : 'save',
						handler:function() {
						    //表单是否被更改的标志
							//var changedFlag = false;
							//changedFlag = addPanel.findById('tax_name').isDirty() || addPanel.findById('tax_rate').isDirty() || //addPanel.findById('tax_deduct').isDirty() || addPanel.findById('fixedRate').isDirty();
							if (!changedFlag) {
								store.each(function(item){
									if (changedFlag || item.dirty){
										changedFlag = true;
									}
								});
							}
							//表单没有被更改
							if (!changedFlag){
								addWin.hide();
								return;
							}
							//校验表单合法性
							if (!addPanel.findById('tax_name').validate()){
								addPanel.findById('tax_name').focus();
								return;
							}
							if (!addPanel.findById('tax_rate').validate()){
								addPanel.findById('tax_rate').focus();
								return;
							}
							if (!addPanel.findById('tax_deduct').validate()){
								addPanel.findById('tax_deduct').focus();
								return;
							}
						    var modified = store.modified.slice(0);
							//校验EditorGrid数据的合法性
							for (var i = 0; i < modified.length; i++) {
								var record = modified[i];
								var fields=record.fields.keys;
								//此处从2开始是避免了没有editor的两个隐藏字段
								for (var j = 2; j<fields.length; j++) {
									var name = fields[j];
									var value = record.data[name];
									
									var colIndex = cm.findColumnIndex(name);
									var rowIndex = store.indexOfId(record.id);
									var editor = cm.getCellEditor(colIndex).field;
		
									if (value != null && !editor.validateValue(value)){
										Ext.Msg.alert('提示','输入的数据不正确',function(){
											entryGrid.startEditing(rowIndex,colIndex);
										});
										return;
									}
								}
							}
							
							var modifiedArray = [];
							//将EditorGird数据转换成JSON
							Ext.each(modified,function(item){
								modifiedArray.push(item.data);
							});
							
							Ext.Ajax.request({
								url:'Tax!saveTax.action',
								method:'post',
								params:{
									tax:"{\"tax\":{" + "\"tax_code\":\"" + addPanel.findById("tax_code").getValue()+"\",\"tax_name\":\"" + addPanel.findById("tax_name").getValue() + "\",\"tax_deduct\":\"" + addPanel.findById("tax_deduct").getValue() + "\",\"tax_rate\":\"" + addPanel.findById("tax_rate").getValue() + "\",\"used\":\"true\",\"details\":{\"modified\":" + Ext.encode(modifiedArray) + ",\"deleted\":" + Ext.encode(deletedJson) + "}}}"
								},
								success: function(){
										store.commitChanges();	
										taxesstore.on('beforeload' , function(){
											taxesstore.baseParams = {}
										});
										taxesstore.reload();
										addWin.hide();
									},
								failure: function(){
										Ext.Msg.alert('警告','由于异常原因，提交数据失败！');
									}
							});
							
						}
					} , {
						text : '取消',
						id : 'cancel',
						handler : function() {
							//alert(changedFlag);
							//changedFlag = addPanel.findById('tax_name').isDirty() || addPanel.findById('tax_rate').isDirty() || //addPanel.findById('tax_deduct').isDirty() || addPanel.findById('fixedRate').isDirty();
							if (!changedFlag) {
								store.each(function(item){
									if (changedFlag || item.dirty){
										changedFlag = true;
									}
								});
							}
							if (changedFlag){
								Ext.MessageBox.show({
									title:'提示',
									msg:'表单的数据已经被修改，您确认需要放弃修改吗？',
									buttons:Ext.MessageBox.OKCANCEL,
									fn:function(btn) {
										if (btn == "ok"){
											addWin.hide();
										}
										if (btn == "cancel"){
											return false;
										}
									}
								});	
							} else {
								addWin.hide();
							}
						}
					}]
				});
				return addWin;
			}

			function addOnChangedEvent(panel) {
				if (panel == null || typeof(panel) != 'object' || panel.getXType() != 'form') {
					return;	
				}
				var fields = panel.findByType('field',false);
				for (var i = 0; i < fields.length ; i++) {
					var field = fields[i];
					field.on({
						change : function(f,n,o){
							changedFlag = true;
						}
					});
				}
			}